﻿using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using Volo.Abp.DependencyInjection;

namespace Colorful.Furniture.FinanceSdk
{
    /// <summary>
    ///  帮助类
    /// </summary>
    public class FinanceHelper : ITransientDependency
    {
        /// <summary>
        /// 获取文本
        /// </summary>
        /// <param name="slice"></param>
        /// <returns></returns>
        public string GetContentFromSlice(long slice)
        {
            var length = FinanceAdapter.GetSliceLen(slice);
            var bytes = new byte[length];
            var ptr = FinanceAdapter.GetContentFromSlice(slice);
            Marshal.Copy(ptr, bytes, 0, bytes.Length);
            return Encoding.UTF8.GetString(bytes);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="privateKey"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public string Decrypt(string privateKey, string text)
        {
            var rsa = new RSACryptoServiceProvider();
            var bytes = Convert.FromBase64String(text);
            rsa.FromXmlString(privateKey);
            var result = rsa.Decrypt(bytes, false);
            return Encoding.UTF8.GetString(result);
        }


    }
}
